package com.aratek.salary.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

/**
 * @author Yvan Jiang
 * @version 0.0.1
 * @Title ExceptionHandler
 * @note 统一异常处理类
 * @note Copyright 2016 by Aratek . All rights reserved
 * @time 2017/7/27
 **/
@RestController
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    private static final Log LOG = LogFactory.getLog(GlobalExceptionHandler.class);

    @ExceptionHandler(value = RRException.class)
    @ResponseBody
    public Object sysErrorHandler( RRException e){
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("code", e.getCode());
        map.put("msg", e.getMsg());
        LOG.error(e.getCode()+ " : " +e.getMsg());
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    
    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public Object defaultErrorHandler( Throwable e){
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("code",500);
        map.put("msg",e.getMessage());
        LOG.error("error happened",e);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }
}
